package datadasetest;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;

import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class del extends JInternalFrame {
	Connection conn;
	private JLabel label;
	private JTextField textField;
	private JButton button;
	private JTextField textField_1;
	private JLabel label_1;
	private JButton button_1;
	private JTextField textField_2;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField code;
	private JButton button_2;
	private JTextField ea;
	private JLabel label_4;
	private JButton button_3;
	private JTextField ss;
	private JLabel label_5;
	private JLabel label_6;

	public del(Connection conn) {
		super("del", true, true, false, false);
		this.conn = conn;
		setBounds(100, 100, 314, 219);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getTextField());
		getContentPane().add(getButton());
		getContentPane().add(getTextField_1());
		getContentPane().add(getLabel_1());
		getContentPane().add(getButton_1());
		getContentPane().add(getTextField_2());
		getContentPane().add(getLabel_2());
		getContentPane().add(getLabel_3());
		getContentPane().add(getCode());
		getContentPane().add(getButton_2());
		getContentPane().add(getEa());
		getContentPane().add(getLabel_4());
		getContentPane().add(getButton_3());
		getContentPane().add(getSs());
		getContentPane().add(getLabel_5());
		getContentPane().add(getLabel_6());

	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("code");
			label.setBounds(0, 0, 0, 0);
		}
		return label;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(0, 0, 0, 0);
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("검색");
			button.setBounds(0, 0, 0, 0);
		}
		return button;
	}

	private JTextField getTextField_1() {
		if (textField_1 == null) {
			textField_1 = new JTextField();
			textField_1.setBounds(0, 0, 0, 0);
			textField_1.setColumns(10);
		}
		return textField_1;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("ea");
			label_1.setBounds(0, 0, 0, 0);
		}
		return label_1;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("수정");
			button_1.setBounds(0, 0, 0, 0);
		}
		return button_1;
	}

	private JTextField getTextField_2() {
		if (textField_2 == null) {
			textField_2 = new JTextField();
			textField_2.setBounds(0, 0, 0, 0);
			textField_2.setColumns(10);
		}
		return textField_2;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("시리얼");
			label_2.setBounds(0, 0, 0, 0);
		}
		return label_2;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("code");
			label_3.setBounds(24, 14, 57, 15);
		}
		return label_3;
	}

	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(65, 11, 116, 21);
		}
		return code;
	}

	private JButton getButton_2() {
		if (button_2 == null) {
			button_2 = new JButton("검색");
			button_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(ss.getText());
					String sql = "select * from ptbl where serial = ?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);

						ResultSet rs = pstmt.executeQuery();
						if (rs.next()) {// 데이터가 있다면
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));// 코드와 데이터 뿌려준다
						} else {
							JOptionPane
									.showMessageDialog(null, "삭제할 데이터가 없습니다");
							code.setText("");
							ea.setText("");

						}
						rs.close();
						pstmt.close();
					} catch (Exception ex) {
						ex.printStackTrace();

					}

				}
			});
			button_2.setBounds(193, 10, 97, 23);
		}
		return button_2;
	}

	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(65, 39, 116, 21);
		}
		return ea;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("ea");
			label_4.setBounds(24, 39, 57, 15);
		}
		return label_4;
	}

	private JButton getButton_3() {
		if (button_3 == null) {
			button_3 = new JButton("삭제");
			button_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(ss.getText());
					String sql = "delete from ptbl where serial = ?";
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						pstmt.executeUpdate();
						
						pstmt.close();
						JOptionPane
						.showMessageDialog(null, "데이터가 삭제 되었습니다");
					} catch (Exception ex) {
						ex.printStackTrace();

					}

				
				}
			});
			button_3.setBounds(65, 70, 116, 21);
		}
		return button_3;
	}

	private JTextField getSs() {
		if (ss == null) {
			ss = new JTextField();
			ss.setColumns(10);
			ss.setBounds(93, 112, 116, 21);
		}
		return ss;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("시리얼");
			label_5.setBounds(24, 115, 57, 15);
		}
		return label_5;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("검색할 코드를 입력하세요....");
			label_6.setBounds(24, 153, 285, 15);
		}
		return label_6;
	}
}
